package com.zhj.leetcode;

import java.util.Stack;

/**
 * 力扣 20 是否是合法括号
 * @author zhj
 */
public class Test20 {
    public static void main(String[] args) {
        String s = "(";
        System.out.println(isT(s));
    }
    public static boolean isT(String s) {
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{') {
                stack.push(chars[i]);
            } else {
                if (stack.size() == 0) {
                    return false;
                } else {
                    Character pop = stack.pop();
                    if (chars[i] == ')') {
                        if (pop != '(') {
                            return false;
                        }
                    } else if (chars[i] == ']') {
                        if (pop != '[') {
                            return false;
                        }
                    } else if (chars[i] == '}') {
                        if (pop != '{') {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }
            }
        }
        if (stack.size() == 0) {
            return true;
        }
        return false;
    }
}
